define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'bootstrap-table-jumpto'], function ($, undefined, Backend, Table, Form, ShowJumpto) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ads/ad/index' + location.search,
                    add_url: 'ads/ad/add',
                    detail_url: 'ads/ad/detail',
                    goldlog_url: 'ads/gold/index',
                    edit_url: 'ads/ad/edit',
                    del_url: 'ads/ad/del',
                    multi_url: 'ads/ad/multi',
                    check_url: 'ads/ad/check',
                    table: 'self_ad',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pageList: [10, 20, 50],
                showExport: true,
                showJumpto: true,
                searchFormVisible: true,
                columns: [
                    [{
                            checkbox: true
                        },
                        {
                            field: 'id',
                            title: __('Id'),
                            searchable: false
                        },
                        {
                            field: 'user_id',
                            title: __('User_id'),
                            operate: false,
                            searchable: false
                        },
                        {
                            field: 'title',
                            title: '标题',
                            operate: false,
                            searchable: false
                        },
                        // {
                        //     field: 'content',
                        //     title: '内容',
                        //     operate: false,
                        //     searchable: false,
                        //     formatter: function(v,r,i){
                        //         return '<code style="width:240px;word-break: break-all;white-space: normal;">'+v+'</code>'
                        //     }
                        // },
                        {
                            field: 'images',
                            title: '附件',                            
                            operate: false,
                            searchable: false,
                            formatter: function(v,r,i){
                                var html = ''
                                if(r.category == 2){
                                    // for (var k = 0; k < v.length; k++) {
                                    //     html += '<a href="'+v[k]+'" target="_blank"><img class="img-sm img-center" src="'+v[k]+'"></a>';
                                    // }
                                    html += '<a href="'+v[0]+'" target="_blank"><img class="img-sm img-center" src="'+v[0]+'"></a>';
                                }else{
                                    if(r.category == 1){
                                        for (var k = 0; k < v.length; k++) {
                                            html += '<a href="'+v[k]+'" target="_blank"><video class="img-sm img-center" src="'+v[k]+'"></video></a>';
                                        }
                                    }
                                }

                                return html
                            },
                        },
                        {
                            field: 'telephone',
                            title: __('联系方式'),
                            searchable: false,
                            formatter: function(v,r,i){
                                var html = '';
                                if(r.linkname){
                                    html += '<p>联系人：'+r.linkname+'</p>'
                                }
                                if(r.telephone){
                                    html += '<p>电话：'+r.telephone+'</p>'
                                }
                                return html
                            }
                        },
                        {
                            field: 'type',
                            title: __('Type'),
                            searchList: {
                                "1": __('Type 1'),
                                "2": __('Type 2')
                            },
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'address',
                            title: __('地区'),
                            searchable: false,
                            formatter: function(v,r,i){
                                return v ? v : '未知'
                            }
                        },
                        {
                            field: 'money_amount',
                            title: __('赏金总额'),
                            operate: 'BETWEEN',
                            sortable: true,
                        },
                        {
                            field: 'money_count',
                            sortable: true,
                            title: __('赏金份数'),
                            operate: 'BETWEEN',
                        },
                        {
                            field: 'gold_num',
                            sortable: true,
                            title: __('总金币'),
                            operate: 'BETWEEN',
                        },
                        {
                            field: 'get_num',
                            sortable: true,
                            title: __('已被领取金币'),
                            operate: 'BETWEEN',
                        },
                        {
                            field: 'money_amount',
                            title: __('支付状态'),
                            searchable: false,
                            formatter: function(v,r,i){
                                if(r.money_amount <= 0){
                                    return '<span class="text-success">无需支付</span>';
                                }
                                if(r.is_pay_done){
                                    return '<span class="text-success">已支付</span>';
                                }
                                return '<span class="text-danger">待支付</span>';
                            }
                        },
                        {
                            field: 'click_count',
                            title: __('阅览次数'),
                            sortable: true,
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {
                                "0": __('Status 0'),
                                "1": __('Status 1'),
                                "2": __('Status 2')
                            },
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'display',
                            title: __('Display'),
                            searchList: {
                                "0": __('Display 0'),
                                "1": __('Display 1')
                            },
                            formatter: Table.api.formatter.toggle,
                            visible: function (row) {
                                //返回true时按钮显示,返回false隐藏
                                return row.status == 1;
                            }
                        },
                        {
                            field: 'sort',
                            title: __('排序'),
                            operate: 'RANGE',
                            formatter: Table.api.formatter.edit
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'toptime',
                            title: __('置顶时间'),
                            sortable: true,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'effecttime',
                            title: __('到期时间'),
                            sortable: true,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'recommendtime',
                            title: __('推荐时间'),
                            operate: 'RANGE',
                            sortable: true,
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.buttons,
                            buttons: [{
                                    name: 'del',
                                    title: __('Del'),
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    icon: 'fa fa-trash',
                                    url: $.fn.bootstrapTable.defaults.extend.del_url,
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        Layer.alert(ret.msg);
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return false;
                                    }
                                },
                                {
                                    name: 'detail',
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-navicon',
                                    url: $.fn.bootstrapTable.defaults.extend.detail_url,
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        Layer.alert(ret.msg);
                                    },
                                    visible: function (row) {
                                        return true
                                    }
                                },
                                {
                                    name: 'gold',
                                    title: __('金币领取日志'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-list',
                                    url: $.fn.bootstrapTable.defaults.extend.goldlog_url + '?ad_id={id}',
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        Layer.alert(ret.msg);
                                    },
                                    visible: function (row) {
                                        return row.get_num > 0
                                    }
                                },
                                {
                                    name: 'refuse',
                                    title: __('审核拒绝'),
                                    classname: 'btn btn-xs btn-primary btn-prompt',
                                    extend: ' data-prompt="2" data-datakey="reason" data-table-id="table" placeholder="请输入原因"',
                                    icon: 'fa fa-times',
                                    url: $.fn.bootstrapTable.defaults.extend.check_url + "?action=refuse",
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        Layer.alert(ret.msg);
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.money_amount > 0){
                                            return row.is_pay_done && row.status == 0
                                        }
                                        return row.status == 0;
                                    }
                                },
                                {
                                    name: 'check',
                                    title: __('审核通过'),
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    icon: 'fa fa-check',
                                    url: $.fn.bootstrapTable.defaults.extend.check_url + "?action=check",
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        Layer.alert(ret.msg);
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.money_amount > 0){
                                            return row.is_pay_done && row.status == 0
                                        }
                                        return row.status == 0;
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});